package com.have.blog.web.service.impl;

import com.have.blog.base.constant.BaseMessageConstant;
import com.have.blog.base.dto.SystemConfigDto;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.base.exception.QueryException;
import com.have.blog.model.mapper.SystemConfigMapper;
import com.have.blog.model.models.SystemConfig;
import com.have.blog.model.models.SystemConfigExample;
import com.have.blog.utils.MyBeanUtils;
import com.have.blog.web.service.SystemConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@SuppressWarnings("all")
public class SystemConfigServiceImpl implements SystemConfigService {
    @Autowired
    private SystemConfigMapper systemConfigMapper;
    @Override
    public SystemConfigDto getConfig() {
        SystemConfigExample example = new SystemConfigExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        example.setOrderByClause("create_time desc");
        List<SystemConfig> systemConfigList = systemConfigMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(systemConfigList)) {
            throw new QueryException(BaseMessageConstant.SYSTEM_CONFIG_IS_NOT_EXIST);
        }
        SystemConfig systemConfig = systemConfigList.get(0);
        SystemConfigDto systemConfigDto = new SystemConfigDto();
        MyBeanUtils.copyProperties(systemConfig, systemConfigDto);
        return systemConfigDto;
    }
}
